package com.xiaorun.portal.spider.processer;

import com.xiaorun.portal.modal.Vo.Enterprise;
import org.apache.commons.codec.digest.DigestUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class EnterprisePageProcesser implements PageProcessor {

    private Site site = Site.me().setDomain("gdsmp.gdyz.org.cn");
    // 部分一：抓取网站的相关配置，包括编码、抓取间隔、重试次数等
//    private Site site = Site.me().setDomain("gdsmp.gdyz.org.cn").setRetryTimes(3).setSleepTime(1000).setTimeOut(600000).addHeader("Accept-Encoding", "/").setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36");

    @Override
    public Site getSite() {
        return site;
    }

    @Override
    public void process(Page page) {
       // List<String> links = page.getHtml().links().regex("http://my\\.oschina\\.net/flashsword/blog/\\d+").all();

        //加入满足条件的链接
        List<String> links = page.getHtml().xpath("//div[@class='Page']/div/a[@class='Page_Next']/@href").all();
        page.addTargetRequests(links);

        List<Selectable> selectableList = page.getHtml().xpath("//*[@id=\"panTrue\"]/table/tbody/tr").nodes();
        if(selectableList != null && selectableList.size() > 0){
            List<Enterprise> enterpriseList = new ArrayList<>(50);
            for(Selectable selectable : selectableList){
                Enterprise enterprise = new Enterprise();
                String year = selectable.xpath("//td[1]/text()").toString().trim();
                String name = selectable.xpath("//td[2]/text()").toString().trim();
                String trademark = selectable.xpath("//td[3]/text()").toString().trim();
                String company = selectable.xpath("//td[4]/text()").toString().trim();
                String address = selectable.xpath("//td[5]/text()").toString().trim();
                String expiryDate = selectable.xpath("//td[6]/text()").toString().trim();
                StringBuilder md5 = new StringBuilder();
                md5.append(year).append(name).append(trademark).append(company).append(address);

                enterprise.setYear(Integer.parseInt(Pattern.compile("[^0-9]").matcher(year).replaceAll("")));
                enterprise.setName(name);
                enterprise.setTrademark(trademark);
                enterprise.setCompany(company);
                enterprise.setAddress(address);
                enterprise.setExpiryDate(Integer.parseInt(Pattern.compile("[^0-9]").matcher(expiryDate).replaceAll("")));
                enterprise.setMd5(DigestUtils.md5Hex(md5.toString()));

                enterpriseList.add(enterprise);
            }
            page.putField("enterpriseList", enterpriseList);
        }
    }

    public static void main(String[] args) {
        //http://gdsmp.gdyz.org.cn/sitecn/aspx/productquery.aspx?keyvalue=2017&typename=year
        Spider.create(new EnterprisePageProcesser()).addUrl("http://gdsmp.gdyz.org.cn/sitecn/aspx/productquery.aspx?keyvalue=2017&typename=year").addPipeline(new ConsolePipeline()).run();
    }
}
